<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class UsersTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	
	public function saveUserData($users)
    {
		//$password=md5($users['password']);
		$data = array(
			//'user_name' 	  	       => $users['user_name'],		
			//'email' 		           => $users['email'],  		
			//'password' 		       => $password,		
			'phone' 		           => $users['phonenumber'],			
			'device_id' 		       => $users['deviceID'],			
			'lat' 		           	   => $users['latitude'],			
			'long' 		           	   => $users['longitude'],			
			'platform' 		           => $users['platform'],			
			//'address' 		       => $users['address'],		
			//'city' 		           => $users['city'],		
			'pincode' 		       	   => 0,		
			//'image' 		           => $users['image'],		
			'addeddate' 		       => date('Y-m-d H:i:s'),	
			'updateddate' 		       => date('Y-m-d H:i:s'),		
			'status'  	               => 1,  				
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;		
    }		
    
	public function check_User_Phonenumber($pnumber){        
		$select = $this->tableGateway->getSql()->select()
					->join('user_settings', new Expression('users.user_id=user_settings.user_id'),array('rating','radius','setting_rating'),'left')		
					->where('users.phone="'.$pnumber['phonenumber'].'"');                                         
	   $resultSet = $this->tableGateway->selectWith($select);                            
	   return $resultSet;
	}
	
	public function checkloginEmail($data){  
		$select = $this->tableGateway->getSql()->select();                        
	    $select->where('email="'.$data['email'].'"');                                         
	    $select->where('password="'.md5($data['password']).'"');
	    $select->where('status="1"');                                         
		$resultSet = $this->tableGateway->selectWith($select); 
	    return $resultSet;
	}
	public function getUserData($user_id){        
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_settings', new Expression('users.user_id=user_settings.user_id'),array('*'),'left');
		$select->where('users.user_id="'.$user_id.'"');                                         
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
    }
	public function updateUserFields($user_id,$userfields){ 
		if(isset($userfields['userName'])){
			$data = array(
				'user_name' 	    => $userfields['userName'],
				'address' 	   	    => $userfields['userAddress'],				
				'pincode' 	   	    => $userfields['userPincode'],				
				'updateddate'		=> date('Y-m-d H:i:s'),
			);
		}else if(isset($userfields['latitude']) && ($userfields['longitude'])){
			$data = array(
				'lat' 	   	   		=> $userfields['latitude'], 
				'long' 	   	    	=> $userfields['longitude'], 
				'updateddate'		=> date('Y-m-d H:i:s'),
			);
		}
		$updaed_status=$this->tableGateway->update($data, array('user_id' => $user_id));
		return 	$updaed_status;		
	}
	public function updateUserData($user_id,$userfields){ 
		$data = array(
			'lat' 	   	   		=> $userfields['latitude'], 
			'long' 	   	    	=> $userfields['longitude'],
			'device_id' 		=> $userfields['deviceID'],				
			'updateddate'		=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_id' => $user_id));
		return 	$updaed_status;		
	}
}